<?php
  header('content-type:text/html;charset=utf-8');
  include_once './lib/fun.php';
  $login = checkLogin();
  if ($login) {
      $user = $_SESSION['user'];
  }
  // 分页
  $con = mysqlInit('127.0.0.1', 'root', 'root', 'mall');
  if ($con) {
      $page = isset($_GET['page']) && is_numeric($_GET['page'])?intval($_GET['page']):1;
      $page = max(1, $page);
      $pageSize = 1;
      $pageOffset = ($page - 1) * $pageSize;
      $sql = "SELECT `id`,`name`,`pic`,`des` FROM `mall_goods` ORDER BY `id` DESC,`view` ASC LIMIT {$pageOffset},{$pageSize}";
      $res = mysql_query($sql);
      $goods = array();
      while ($result = mysql_fetch_assoc($res)) {
          $goods[] = $result;
      }
      unset($sql,$res);
      $sql = "SELECT count(`id`) as 'total' from `mall_goods`";
      $res = mysql_query($sql);
      $result = mysql_fetch_assoc($res);
      $total = isset($result['total']) ? $result['total'] : 0;
      $pageHtml = pages($total, $page, $pageSize, 5);
  }
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>M-GALLARY|首页</title>
    <link rel="stylesheet" type="text/css" href="./static/css/common.css"/>
    <link rel="stylesheet" type="text/css" href="./static/css/index.css"/>
</head>
<body>
<div class="header">
    <div class="logo f1">
        <img src="./static/image/logo.png">
    </div>
    <div class="auth fr">
        <ul>
            <?php if (!$login): ?>
            <li><a href="login.php">登录</a></li>
            <li><a href="register.php">注册</a></li>
          <?php else: ?>
            <li>管理员：<?php echo $user['username']; ?></li>
            <li><a href="publish.php">发布</a></li>
            <li><a href="logout.php">退出</a></li>
          <?php endif; ?>
        </ul>
    </div>
</div>
<div class="content">
    <div class="banner">
        <img class="banner-img" src="./static/image/welcome.png" width="732px" height="372" alt="图片描述">
    </div>
    <div class="img-content">
        <ul>
          <?php foreach ($goods as $v): ?>
            <li>
                <img class="img-li-fix" src="<?php echo '.'.$v['pic'] ?>" alt="">
                <div class="info">
                    <a href="detail.php?id=<?php echo $v['id'] ?>"><h3 class="img_title"><?php echo $v['name']; ?></h3></a>
                    <p>
                       <?php echo $v['des']; ?>
                    </p>
                    <div class="btn">
                        <a href="edit.php?id=<?php echo $v['id']?>" class="edit">编辑</a>
                        <a href="delete.php?id=<?php echo $v['id']; ?>" class="del">删除</a>
                    </div>
                </div>
            </li>
            <?php endforeach; ?>
        </ul>
    </div>
    <?php echo $pageHtml; ?>
</div>

<div class="footer">
    <p><span>M-GALLARY</span>©2017 POWERED BY IMOOC.INC</p>
</div>
</body>
<script src="./static/js/jquery-1.10.2.min.js"></script>
<script>
    $(function () {
        $('.del').on('click',function () {
            if(confirm('确认删除该画品吗?'))
            {
               window.location = $(this).attr('href');
            }
            return false;
        })
    })
</script>


</html>
